package com.zang.blogz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.entity.Job;
import com.zang.blogz.model.input.ro.JobRO;
import com.zang.blogz.model.input.ro.JobRunRO;
import com.zang.blogz.model.input.ro.JobSearchRO;
import com.zang.blogz.model.input.ro.JobStatusRO;
import com.zang.blogz.model.output.JobVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 定时任务调度表 前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-09-04
 */
@RestController
@RequestMapping("/blogz/job")
@Api(tags = "定时任务模块")
public class JobController {

    @Autowired
    private JobService jobService;

    @OptLog(optType = OptTypeConstant.SAVE)
    @ApiOperation(value = "添加定时任务", notes = "添加定时任务")
    @PostMapping("/admin/save/jobs")
    public Result<JobRO> saveJob(@RequestBody JobRO jobRO){

        return jobService.saveJob(jobRO);
    }

    @OptLog(optType = OptTypeConstant.UPDATE)
    @ApiOperation(value = "修改定时任务", notes = "修改定时任务")
    @PostMapping ("/admin/update/jobs")
    public Result<JobRO> updateJob(@RequestBody JobRO jobRO) {

        return jobService.updateJob(jobRO);

    }

    @OptLog(optType = OptTypeConstant.REMOVE)
    @ApiOperation(value = "批量删除定时任务", notes = "批量删除定时任务")
    @DeleteMapping("/admin/deleteByIds")
    public Result<Boolean> deleteByIds(@RequestBody List<Integer> jobIds){

        return jobService.deleteByIds(jobIds);
    }

    @ApiOperation(value = "获取定时任务列表", notes = "获取定时任务列表")
    @GetMapping("/admin/")
    public Result<IPage<JobVO>> listJobs(@RequestBody JobSearchRO jobSearchRO,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<Job> page = new Page<>(pageNo, pageSize);
        return Result.ok(jobService.listJobs(jobSearchRO, page));
    }

    @ApiOperation(value = "更改任务的状态", notes = "更改任务的状态")
    @PostMapping("/admin/update/jobsStatus")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<JobStatusRO> updateJobStatus(@RequestBody JobStatusRO jobStatusRO) {


        return jobService.updateJobStatus(jobStatusRO);
    }

    @ApiOperation(value = "执行某个任务", notes = "执行某个任务")
    @PostMapping("/admin/run/jobs")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<Boolean> runJob(@RequestBody JobRunRO jobRunRO) {

        return jobService.runJob(jobRunRO);
    }

    @ApiOperation(value = "获取所有job分组", notes = "获取所有job分组")
    @GetMapping("/admin/jobGroups")
    public Result<List<String>> listJobGroup() {

        return Result.ok(jobService.listJobGroup());
    }

    @ApiOperation(value = "删除定时任务", notes = "删除定时任务")
    @DeleteMapping("/admin/deleteById")
    @OptLog(optType = OptTypeConstant.REMOVE)
    public Result<Boolean> deleteById(@RequestParam Integer id){

        return jobService.deleteById(id);
    }





}
